package hotel.quartz.oversea.config;

import hotel.base.oversea.constant.MeituanConstants;
import hotel.quartz.oversea.config.service.RedisService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

@Slf4j
@Component
public class ApplicationRunnerImpl implements ApplicationRunner {
    /*@Autowired
    private JobAndTriggerService jobAndTriggerService;*/
    @Resource
    private RedisService redisService;
    @Override
    public void run(ApplicationArguments args) throws Exception {
        log.info("通过实现ApplicationRunner接口，在quart spring boot项目启动后，初始化指定定时器");
        String[] sourceArgs = args.getSourceArgs();

        redisService.remove( MeituanConstants.REDIS_KEY_BOOKING_JOB_ALL_WAIT_ORDER);
        log.info("服务重启 重置缓存 MeituanConstants.REDIS_KEY_BOOKING_JOB_ALL_WAIT_ORDER:size={}",redisService.queueSize(MeituanConstants.REDIS_KEY_BOOKING_JOB_ALL_WAIT_ORDER));

        redisService.remove( MeituanConstants.REDIS_KEY_DEALING_NOTIFY_ROOM_STATE_JOB_PROCESS_ING);
        redisService.remove( MeituanConstants.REDIS_KEY_CHNAGE_PRICE_JOB_PROCESS_ING);
        redisService.remove( MeituanConstants.REDIS_KEY_DEALING_NOTIFY_ROOM_INFO_JOB_PROCESS_ING);
        redisService.remove(MeituanConstants.REDIS_KEY_CHNAGE_PRICE_JOB_PROCESS_ING);
        redisService.remove("doing_PartTimeQueryCheckStatusJob");
        redisService.remove("doing_PartTimeQueryCheckPriceJob");
        redisService.remove("BasicMtGoodsInfoCheckUpdateJob_doing");


        //获取通知单定时器 默认是往前推1分钟，重启时，首次，往前推五分钟
        redisService.setUnit("SwitchHotelOrderGetNotifyJob_beforSecond",60*5,12L, TimeUnit.SECONDS);




    /*    for (String arg : sourceArgs) {
            System.out.print(arg + " ");
        }
        System.out.println();*/

        /*Map<String,String> map = new HashMap();
        map.put("hotel.quartz.job.meituan.MeituanHotelDealNotifyGoodsJob","Meituan");
        map.put("hotel.quartz.job.meituan.MeituanHotelDealNotifyGoodsStateJob","Meituan");
        map.put("hotel.quartz.job.meituan.MeituanSaleHotelGoodsPriceChangeQueryJob","Meituan");
        for (Map.Entry entry:map.entrySet()){
            String curTime = DateUtil.getCurrTime();
            Date date = DateUtil.addSecond(curTime,5);
            String execuTime = DateUtil.formatDate(date,"ss mm HH");
            JobForm jobForm = new JobForm();
            jobForm.setJobClassName((String)entry.getKey());
            jobForm.setJobGroupName((String)entry.getValue());
            jobForm.setCronExpression(execuTime+" * * ?");
            try {
                jobAndTriggerService.cronJob(jobForm);
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                Thread.sleep(1000);
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }*/
    }
}
